/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Feedback_Menu.java
 *
 * Created on 27 September 2010, 1:45:36 AM
 */

package CAT200;
import java.util.*;
import java.sql.*;
/**
 *
 * @author Wai Hong
 */
public class Feedback_Menu extends javax.swing.JFrame {

    public String title, message;
    private int i;
    private ResultSet result;
    private Vector<Vector<String>> feedbackData;
    private Vector<String> feedbackDetail;
    /** Creates new form Feedback_Menu */
    public Feedback_Menu() {
        try
        {
            //feedbackData = getFeedbackTable(First_Menu.UserAdmin);
            feedbackData = getFeedbackTable();

            feedbackDetail = new Vector<String>();
            feedbackDetail.add("No.");
            feedbackDetail.add("Title");
            feedbackDetail.add("Message");
            feedbackDetail.add("Date");
            initComponents();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
        catch(Exception e){  }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        CANCEL = new javax.swing.JButton();
        VIEW = new javax.swing.JButton();
        NEW = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Feedback Menu", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N

        CANCEL.setText("Cancel");
        CANCEL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CANCELActionPerformed(evt);
            }
        });

        VIEW.setText("View");
        VIEW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VIEWActionPerformed(evt);
            }
        });

        NEW.setText("New");
        NEW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NEWActionPerformed(evt);
            }
        });

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            feedbackData, feedbackDetail
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(CANCEL)
                        .addGap(18, 18, 18)
                        .addComponent(NEW)
                        .addGap(18, 18, 18)
                        .addComponent(VIEW)
                        .addGap(32, 32, 32))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CANCEL, NEW, VIEW});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(VIEW)
                    .addComponent(NEW)
                    .addComponent(CANCEL))
                .addGap(30, 30, 30))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void CANCELActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        this.dispose();
    }                                      

    private void NEWActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        new MakeFeedbackUser().setVisible(true);
        this.dispose();
    }                                   

    private void VIEWActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        try
        {
            DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");

            Statement stmt = con.createStatement();
            ResultSet rsmt = stmt.executeQuery("select FEEDBACK_TITLE, MESSAGE from FEEDBACK where CUST_USERNAME = '" + First_Menu.UserAdmin + "' order by FEEDBACK_NUM");
            
            for(int a = 0; a <= jTable1.getSelectedRow(); a++)
                rsmt.next();

            title = rsmt.getString("FEEDBACK_TITLE");
            message = rsmt.getString("MESSAGE");

            con.close();
            rsmt.close();
            stmt.close();
        }
        catch(Exception e){  }

        new ViewFeedback(title, message).setVisible(true);
    }                                    
    private Vector getFeedbackTable()throws Exception
    {
        Vector<Vector<String>> feedback = new Vector<Vector<String>>();
        DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");

        Statement stmt = con.createStatement();
        result = stmt.executeQuery("select FEEDBACK_DATE, FEEDBACK_TITLE, MESSAGE from FEEDBACK where CUST_USERNAME = '" + First_Menu.UserAdmin + "' order by FEEDBACK_NUM");
        
        i = 1;
        while(result.next())
        {
            Vector<String> feedbackLine = new Vector<String>();

            feedbackLine.addElement(Integer.toString(i));
            feedbackLine.addElement(result.getString("FEEDBACK_TITLE"));
            feedbackLine.addElement(result.getString("MESSAGE"));
            feedbackLine.addElement(result.getString("FEEDBACK_DATE"));
            feedback.add(feedbackLine);
            i++;
        }

        return feedback;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try
                {
                    new Feedback_Menu().setVisible(true);
                }
                catch(Exception e){  }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton CANCEL;
    private javax.swing.JButton NEW;
    private javax.swing.JButton VIEW;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

}
